<?php
/**
	Sponsor Manager (class.promotionalcontent.php file)
    Copyright (C) <Sun Mar 30 12:47:56 CEST 2008>  Marcos Ramírez <marcosramirez@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    
    Purpose:
    Last Modified:
**/

class PromotionalContent {
	public function __construct() {
	}
	
	public function addPromotionalType ($name, $description) {
		global $db;
		global $html;
		//Check if exists.
		$resultado=$db->query("SELECT name FROM My_Adstypes WHERE name='$name'");
		if ($db->error()!='') {
			$html->addError(_('Error on query').$db->error());
			return true;
		}
		
		if (is_array($Res=$db->fetch())) {
			$html->addError(_('Error: Promotional Type allready added.'));
			return true;
		}
		
		//Add new one
		$resultado=$db->query("INSERT INTO My_Adstypes (name, description) VALUES ('$name', '$description')");
		if ($db->error()!='') {
			$html->addError(_('Error inserting Promotional Type: ').$db->error());
			return true;
		} else 
			return false;
		
			
	}
	
	public function getPromotionalTypesList () {
		global $db;
		$result=$db->query("SELECT * FROM My_Adstypes");
		return $Res=$db->fetchAll();
	}
	
	public function getPromotionalTypesOptions () {
		global $db;
		$result=$db->query("SELECT * FROM My_Adstypes");
		while($Res=$db->fetch())
			$options.='<option value="'.$Res['adtypeid'].'">'.$Res['name'].'</option>';
		return $options;
	}
	

	
	/**
	 * Adds Promotional Content
	 *
	 * @param unknown_type $type
	 * @param unknown_type $content
	 * @param unknown_type $sponsor
	 */
	public function addPromotionalContent ($type, $content, $sponsor, $multiplemode) {
		global $db;
		global $html;
		
		$result=$db->query("SELECT adtype FROM My_Ads 
							WHERE adtype='$type'
							AND adcode='$content' 
							AND sponsorid='$sponsor'");
							
		if ($db->error()!='') {
			$html->addError(_('Error consulting DataBase: ').$db->error());
			return true;
		}
		
		if (is_array($Res=$db->fetch())) {
			$html->addError(_('This promotional Content was allready insert on database'));
			return true;
		}
		
		if ($multiplemode!='on') {
		
			$result=$db->query("INSERT INTO My_Ads (adtype, adcode, sponsorid) VALUES ('$type', '$content', '$sponsor')");
			if ($db->error()!='') {
			$html->addError(_('Error consulting DataBase: ').$db->error());
			return true;
			} else {
			return false;
			}
		} else {
		//Multiple mode
			$Lines=explode("\n", $content);
			foreach ($Lines as $line) {
				$result=$db->query("INSERT INTO My_Ads (adtype, adcode, sponsorid) VALUES ('$type', '$line', '$sponsor')");
				if ($db->error()!='') {
					$html->addError(_('Error consulting DataBase: ').$db->error());
					//return true;
					} else {
						$html->addInfo("$line correctly added");
						//return false;
					}
			}
		}
	}
	
	public function totalPromotionalContentFromSponsor ($id) {
		global $sm;
		$result=$sm->Query("SELECT COUNT(adid) FROM My_Ads WHERE sponsorid='$id'");
		$Res=$sm->Fetch($result);
		return $Res['COUNT(adid)'];
	}

	public function totalPromotionalContent() {
		global $sm;
		$result1=$sm->Query("SELECT sponsorid FROM My_Sponsors");
		while ($Res=$sm->Fetch($result1)) 			
			$return+=$this->totalPromotionalContentFromSponsor($Res['sponsorid']);
		
		return $return;
	}


}
?>